import item

class Unit:
    '''Anything which can be interacted with'''
    def __init__(self,game,uclass,uname=None,ulevel=1):
        self.game = game
        self.uname = uname
        self.uclass = uclass
        if self.uname:
            self.udef = self.game.get_class(self.uclass)
        else:
            self.udef = self.game.get_monster(self.uclass)

        self.ulevel = ulevel
        self.uitems = []
        for item_name in self.udef['items'].split(','):
            self.uitems.append(item.Item(self.game,item_name.strip()))

        self.ustats = {}
        for stat in self.game.stats:
            self.ustats[stat] = 0
        self.uskills = {}
        for skill in self.game.skills:
            self.uskills[skill] = 0
        self.udmg = {}
        self.uarm = {}
        for dmg in self.game.dmgs:
            self.udmg[dmg] = 0
            self.uarm[dmg] = 0

        self.current_hp = float('inf')
        self.current_mp = float('inf')
        self.reset_stats()

        self.umoney = 0
        self.uxp = 0
        self.upos = (0,0)

    def is_alive(self):
        return self.current_hp > 0

    def reset_stats(self):
        for stat in self.ustats:
            self.ustats[stat] = self.calc_stat(stat)
        for dtype in self.udmg:
            self.udmg[dtype] = self.calc_stat('dmg_'+dtype)
            self.uarm[dtype] = self.calc_stat('arm_'+dtype)
        if self.current_hp > self.ustats['hp']:
            self.current_hp = self.ustats['hp']
        if self.current_mp > self.ustats['mp']:
            self.current_mp = self.ustats['mp']

    def gain_xp(self,xp):
        self.uxp += xp
        while self.uxp > 100:
            self.uxp -= 100
            self.ulevel += 1
            self.reset_stats()

    def move(self,mov,new=False):
        if new:
            self.upos = mov
        else:
            self.upos[0] += mov[0]
            self.upos[1] += mov[1]

    def loot(self):
        for item in self.uitems:
            item.unequip()
        self.uitems = []
        self.umoney = 0

    def add_item(self,item):
        self.uitems.append(item)

    def remove_item(self,item):
        if item in self.uitems:
            self.uitems.remove(item)

    def equip(self,item):
        if not item.iequipped:
            item.equip()
            self.reset_stats()

    def unequip(self,item):
        if item.iequipped:
            item.unequip()
            self.reset_stats()

    def string(self,stat):
        if stat == 'hp':
            return '{} / {}'.format(self.current_hp,self.ustats['hp'])
        if stat == 'mp':
            return '{} / {}'.format(self.current_mp,self.ustats['mp'])
        if stat in self.ustats:
            return str(self.ustats[stat])
        if stat == 'dmg':
            return str(sum(self.udmg.values()))
        if stat == 'arm':
            return str(sum(self.uarm.values()))

    def calc_stat(self,stat):
        calc = 0
        if stat in self.udef:
            (base,per_level) = self.udef[stat].split(',')
            calc += float(base.strip()) + self.ulevel * float(per_level.strip())
            if stat == 'hp':
                calc += 2 * self.ustats['str'] + self.ustats['dex'] + 0.5 * self.ustats['int']
            if stat == 'mp':
                calc += 0.5 * self.ustats['str'] + self.ustats['dex'] + 2 * self.ustats['int']
        if stat in self.uskills:
            calc += self.uskills[stat] * float(per_level.strip())
        for item in self.uitems:
            calc += item.get_stat(stat)
        return calc

    # interact
    # attack monster
    # ai if monster (defined in data/)
